﻿using System;

namespace Net.Share
{
    /// <summary>
    /// 标注为远程过程调用函数
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class Rpc : Attribute
    {
        /// <summary>
        /// 网络命令
        /// </summary>
        public byte cmd;
        /// <summary>
        /// 远程方法的哈希值
        /// </summary>
        public uint hash;
        /// <summary>
        /// 远程方法名
        /// </summary>
        public string func;

        /// <summary>
        /// 远程过程调用参数
        /// <para>如果是byte, sbyte类型则是<see cref="Rpc.cmd"/>命令</para>
        /// <para>如果是enum或short, ushort, int, uint, long, ulong则是<see cref="Rpc.hash"/>值</para>
        /// <para>如果是string则是<see cref="Rpc.func"/>值</para>
        /// </summary>
        /// <param name="values"></param>
        public Rpc(params object[] values)
        {
            foreach (var value in values)
            {
                var typeCode = Type.GetTypeCode(value.GetType());
                if (typeCode == TypeCode.Byte | typeCode == TypeCode.SByte)
                {
                    cmd = Convert.ToByte(value);
                }
                else if (typeCode == TypeCode.Int16 | typeCode == TypeCode.UInt16 | typeCode == TypeCode.Int32 | typeCode == TypeCode.UInt32 | typeCode == TypeCode.Int64 | typeCode == TypeCode.UInt64)
                {
                    hash = Convert.ToUInt32(value);
                }
                else if (typeCode == TypeCode.String)
                {
                    func = value.ToString();
                }
            }
        }
    }
}